﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


using Jungle.Code.Utils;
using Jungle.Rent;

namespace Jungle.Alex.WebControls
{
    public class OfferNonActiveList : WebControl, IPostBackEventHandler
    {
        private int OnPage = 100;
        private int CurrPage = 1;

        //------------------------------------------------------------------------------------------------------
        public void RaisePostBackEvent(string eventArgument)
        {
           int pos = eventArgument.IndexOf(',');
           int l = eventArgument.Length;
           string s1 = eventArgument.Substring(0, pos);
           string s2 = eventArgument.Substring(pos + 1, (l - (pos + 1)));

           if (s1 == "chP") // перелистывание страницы
           {
                CurrPage = Convert.ToInt32(s2);
           }
           if (s1 == "delItem") // удаление пользователя
           {
               Offer o = new Offer(); o.OfferId = Convert.ToInt32(s2); o.Delete();
           }   
        }
        //--------------------------------------------------------------------
        protected override void RenderContents(HtmlTextWriter writer)
        {
            Offer offs = new Offer();
            offs.GetAllNonActiv();

            writer.Write("<b>Заявки</b>");
            writer.AddAttribute("class", "tbl0001");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("<b>Код объекта</b>");
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("<b>Дата заявки</b>");
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("<b>Действие</b>");
            writer.RenderEndTag();
            writer.RenderEndTag();

            int count = 1;
            for (int i = (OnPage * CurrPage) - OnPage; i <= (OnPage * CurrPage) - 1; i++)
            {
                if (i >= offs.OfferList.Count) break;
                Offer o = offs.OfferList[i];

                if (count % 2 != 0) writer.AddAttribute("style", "background-color: #eee;");
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, "Modules.aspx?mod=Offers&cmd=view&id=" + o.OfferId.ToString());
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                    writer.Write(Offer.GetObjectNomber(o.OfferId));
                    writer.RenderEndTag();
                    writer.RenderEndTag();

                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write(o.PubDate);
                    writer.RenderEndTag();

                    writer.RenderBeginTag(HtmlTextWriterTag.Td);

                    // ------ Redner delete buttom ---------
                    PrintBtnDel(writer, o.OfferId);
                    // -------------------------------------

                    writer.RenderEndTag();

                writer.RenderEndTag();

                count++;
            }
            writer.RenderEndTag();

            if (offs.OfferList.Count % OnPage == 0)
                RenderPageLister(writer, offs.OfferList.Count / OnPage);
            else
                RenderPageLister(writer, (offs.OfferList.Count / OnPage) + 1);


            base.RenderContents(writer);
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintBtnDel(HtmlTextWriter writer, int itemID)
        {
            ClientScriptManager cs = Page.ClientScript;
            PostBackOptions opt = new PostBackOptions(this);
            opt.ActionUrl = Context.Request.RawUrl;
            opt.Argument = "delItem," + itemID.ToString();

            writer.AddAttribute("onclick", "if(confirm(\"Вы действительно хотите удалить?\"))" + cs.GetPostBackEventReference(opt));
            writer.AddAttribute("title", "Удалить");
            writer.AddAttribute("style", "cursor: pointer;");
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.Write("[Удалить]");
            writer.RenderEndTag();
            return writer;
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintImage(HtmlTextWriter writer, string imgUrl)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Src, UrlHelper.GetSiteUrl() + imgUrl);
            writer.AddAttribute(HtmlTextWriterAttribute.Align, "absmiddle");
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();
            return writer;
        }
        //-----------------------------------------------------------------------------------------------
        private HtmlTextWriter RenderPageLister(HtmlTextWriter writer, int count)
        {
            ClientScriptManager cs = Page.ClientScript;
            writer.Write("<b>Страница: </b>");
            for (int i = 1; i <= count; i++)
            {
                if (i == CurrPage)
                    writer.Write("<b>" + i.ToString() + "</b>");
                else
                {
                    PostBackOptions opt = new PostBackOptions(this);
                    opt.ActionUrl = Context.Request.RawUrl;
                    opt.Argument = "chP," + i.ToString();

                    writer.AddAttribute("onclick", cs.GetPostBackEventReference(opt));
                    writer.AddAttribute("style", "cursor: pointer;");
                    writer.RenderBeginTag(HtmlTextWriterTag.Span);
                    writer.Write(i.ToString());
                    writer.RenderEndTag();
                }
                if (i < count) writer.Write(", ");
            }
            return writer;
        }
    }
}
